<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="scope">
              {{ formatType(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
            :page-size="size"
            :page-sizes="[2,5,10]"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployees } from '@/api/employees'
import EmployeeENUM from '@/constant/employees'
const hireType = EmployeeENUM.hireType.reduce((acc, item) => { return { ...acc, [item.id]: item.value } }, {})
import PageTools from '@/components/PageTools'
export default {
  components: { PageTools },
  data() {
    return {
      list: [],
      page: 1, // 当前页码
      size: 5, // 每页几条
      total: 0 // 总条数
    }
  },
  // 页面打开加载
  created() {
    this.loadEmployees()
  },
  methods: {
    async loadEmployees() {
      // 发送请求
      const res = await getEmployees({ page: this.page, size: this.size })
      console.log('111', res)
      // 数据放入数组
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    // 正式非正式
    formatType(id) {
      return hireType[id] || '未知'
    },
    // 会接收当前点击的页码
    hCurrentChange(curPage) {
      console.log(curPage)
    },
    // 每页几条
    hSizeChange(curSize) {
      console.log(curSize)
    }
  }
}
</script>

<style>

</style>
